{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4fc94f03",
   "metadata": {},
   "source": [
    "# New variables computed by `'count'` and  `'count2d'` statistics\n",
    "\n",
    "`geomBar()` uses `'count'` statistical transformation. In addition to `'..count..'` variable, the statistics now provide additional variables related to sum (`'..sum..'`), `'..prop..'` (proportion) and `'..proppct..'` (proportion in percent).\n",
    "\n",
    "`geomPie()` uses `'count2d'` statistical transformation. It allows to make a slice sizes proportional to the number of cases in each group (or if the weight aesthetic is supplied, the sum of the weights). Also `'count2d'` provides variables for proportion (`'..prop..'`) and proportion in percent (`'..proppct..'`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4b7169bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"PxHbB3\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v3.2.0/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"PxHbB3\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"PxHbB3\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "            <div id=\"kotlin_out_0\"></div>\n",
       "            <script type=\"text/javascript\">\n",
       "                            if(!window.kotlinQueues) {\n",
       "                window.kotlinQueues = {};\n",
       "            }\n",
       "            if(!window.kotlinQueues[\"DataFrame\"]) {\n",
       "                var resQueue = [];\n",
       "                window.kotlinQueues[\"DataFrame\"] = resQueue;\n",
       "                window[\"call_DataFrame\"] = function(f) {\n",
       "                    resQueue.push(f);\n",
       "                }\n",
       "            }\n",
       "            (function (){\n",
       "                var modifiers = [(function(script) {\n",
       "    script.src = \"https://cdn.jsdelivr.net/gh/Kotlin/dataframe@3db46ccccaa1291c0627307d64133317f545e6ae/core/src/main/resources/init.js\"\n",
       "    script.type = \"text/javascript\";\n",
       "})];\n",
       "                var e = document.getElementById(\"kotlin_out_0\");\n",
       "                modifiers.forEach(function (gen) {\n",
       "                    var script = document.createElement(\"script\");\n",
       "                    gen(script)\n",
       "                    script.addEventListener(\"load\", function() {\n",
       "                        window[\"call_DataFrame\"] = function(f) {f();};\n",
       "                        window.kotlinQueues[\"DataFrame\"].forEach(function(f) {f();});\n",
       "                        window.kotlinQueues[\"DataFrame\"] = [];\n",
       "                    }, false);\n",
       "                    script.addEventListener(\"error\", function() {\n",
       "                        window[\"call_DataFrame\"] = function(f) {};\n",
       "                        window.kotlinQueues[\"DataFrame\"] = [];\n",
       "                        var div = document.createElement(\"div\");\n",
       "                        div.style.color = 'darkred';\n",
       "                        div.textContent = 'Error loading resource DataFrame';\n",
       "                        document.getElementById(\"kotlin_out_0\").appendChild(div);\n",
       "                    }, false);\n",
       "                    \n",
       "                    e.appendChild(script);\n",
       "                });\n",
       "            })();\n",
       "            </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "                <style>\n",
       "                :root {\n",
       "    --background: #fff;\n",
       "    --background-odd: #f5f5f5;\n",
       "    --background-hover: #d9edfd;\n",
       "    --header-text-color: #474747;\n",
       "    --text-color: #848484;\n",
       "    --text-color-dark: #000;\n",
       "    --text-color-medium: #737373;\n",
       "    --text-color-pale: #b3b3b3;\n",
       "    --inner-border-color: #aaa;\n",
       "    --bold-border-color: #000;\n",
       "    --link-color: #296eaa;\n",
       "    --link-color-pale: #296eaa;\n",
       "    --link-hover: #1a466c;\n",
       "}\n",
       "\n",
       ":root[theme=\"dark\"], :root [data-jp-theme-light=\"false\"], .dataframe_dark{\n",
       "    --background: #303030;\n",
       "    --background-odd: #3c3c3c;\n",
       "    --background-hover: #464646;\n",
       "    --header-text-color: #dddddd;\n",
       "    --text-color: #b3b3b3;\n",
       "    --text-color-dark: #dddddd;\n",
       "    --text-color-medium: #b2b2b2;\n",
       "    --text-color-pale: #737373;\n",
       "    --inner-border-color: #707070;\n",
       "    --bold-border-color: #777777;\n",
       "    --link-color: #008dc0;\n",
       "    --link-color-pale: #97e1fb;\n",
       "    --link-hover: #00688e;\n",
       "}\n",
       "\n",
       "p.dataframe_description {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe {\n",
       "    font-family: \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n",
       "    font-size: 12px;\n",
       "    background-color: var(--background);\n",
       "    color: var(--text-color-dark);\n",
       "    border: none;\n",
       "    border-collapse: collapse;\n",
       "}\n",
       "\n",
       "table.dataframe th, td {\n",
       "    padding: 6px;\n",
       "    border: 1px solid transparent;\n",
       "    text-align: left;\n",
       "}\n",
       "\n",
       "table.dataframe th {\n",
       "    background-color: var(--background);\n",
       "    color: var(--header-text-color);\n",
       "}\n",
       "\n",
       "table.dataframe td {\n",
       "    vertical-align: top;\n",
       "}\n",
       "\n",
       "table.dataframe th.bottomBorder {\n",
       "    border-bottom-color: var(--bold-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(odd) {\n",
       "    background: var(--background-odd);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(even) {\n",
       "    background: var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:hover {\n",
       "    background: var(--background-hover);\n",
       "}\n",
       "\n",
       "table.dataframe a {\n",
       "    cursor: pointer;\n",
       "    color: var(--link-color);\n",
       "    text-decoration: none;\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover > td a {\n",
       "    color: var(--link-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe a:hover {\n",
       "    color: var(--link-hover);\n",
       "    text-decoration: underline;\n",
       "}\n",
       "\n",
       "table.dataframe img {\n",
       "    max-width: fit-content;\n",
       "}\n",
       "\n",
       "table.dataframe th.complex {\n",
       "    background-color: var(--background);\n",
       "    border: 1px solid var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe .leftBorder {\n",
       "    border-left-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightBorder {\n",
       "    border-right-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightAlign {\n",
       "    text-align: right;\n",
       "}\n",
       "\n",
       "table.dataframe .expanderSvg {\n",
       "    width: 8px;\n",
       "    height: 8px;\n",
       "    margin-right: 3px;\n",
       "}\n",
       "\n",
       "table.dataframe .expander {\n",
       "    display: flex;\n",
       "    align-items: center;\n",
       "}\n",
       "\n",
       "/* formatting */\n",
       "\n",
       "table.dataframe .null {\n",
       "    color: var(--text-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe .structural {\n",
       "    color: var(--text-color-medium);\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .dataFrameCaption {\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .numbers {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe td:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "\n",
       "                </style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot\n",
    "%use dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1595f4ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.4.4.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.3.2.0."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b3ba4a8",
   "metadata": {},
   "source": [
    "### 1. Data preparation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "417bc215",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/kotlindataframe+json": "{\"nrow\":5,\"ncol\":12,\"columns\":[\"untitled\",\"manufacturer\",\"model\",\"displ\",\"year\",\"cyl\",\"trans\",\"drv\",\"cty\",\"hwy\",\"fl\",\"class\"],\"kotlin_dataframe\":[{\"untitled\":1,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":18,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":2,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"manual(m5)\",\"drv\":\"f\",\"cty\":21,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":3,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"manual(m6)\",\"drv\":\"f\",\"cty\":20,\"hwy\":31,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":4,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"auto(av)\",\"drv\":\"f\",\"cty\":21,\"hwy\":30,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":5,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.8,\"year\":1999,\"cyl\":6,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":16,\"hwy\":26,\"fl\":\"p\",\"class\":\"compact\"}]}",
      "text/html": [
       "        <html>\n",
       "        <head>\n",
       "            <style type=\"text/css\">\n",
       "                :root {\n",
       "    --background: #fff;\n",
       "    --background-odd: #f5f5f5;\n",
       "    --background-hover: #d9edfd;\n",
       "    --header-text-color: #474747;\n",
       "    --text-color: #848484;\n",
       "    --text-color-dark: #000;\n",
       "    --text-color-medium: #737373;\n",
       "    --text-color-pale: #b3b3b3;\n",
       "    --inner-border-color: #aaa;\n",
       "    --bold-border-color: #000;\n",
       "    --link-color: #296eaa;\n",
       "    --link-color-pale: #296eaa;\n",
       "    --link-hover: #1a466c;\n",
       "}\n",
       "\n",
       ":root[theme=\"dark\"], :root [data-jp-theme-light=\"false\"], .dataframe_dark{\n",
       "    --background: #303030;\n",
       "    --background-odd: #3c3c3c;\n",
       "    --background-hover: #464646;\n",
       "    --header-text-color: #dddddd;\n",
       "    --text-color: #b3b3b3;\n",
       "    --text-color-dark: #dddddd;\n",
       "    --text-color-medium: #b2b2b2;\n",
       "    --text-color-pale: #737373;\n",
       "    --inner-border-color: #707070;\n",
       "    --bold-border-color: #777777;\n",
       "    --link-color: #008dc0;\n",
       "    --link-color-pale: #97e1fb;\n",
       "    --link-hover: #00688e;\n",
       "}\n",
       "\n",
       "p.dataframe_description {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe {\n",
       "    font-family: \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n",
       "    font-size: 12px;\n",
       "    background-color: var(--background);\n",
       "    color: var(--text-color-dark);\n",
       "    border: none;\n",
       "    border-collapse: collapse;\n",
       "}\n",
       "\n",
       "table.dataframe th, td {\n",
       "    padding: 6px;\n",
       "    border: 1px solid transparent;\n",
       "    text-align: left;\n",
       "}\n",
       "\n",
       "table.dataframe th {\n",
       "    background-color: var(--background);\n",
       "    color: var(--header-text-color);\n",
       "}\n",
       "\n",
       "table.dataframe td {\n",
       "    vertical-align: top;\n",
       "}\n",
       "\n",
       "table.dataframe th.bottomBorder {\n",
       "    border-bottom-color: var(--bold-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(odd) {\n",
       "    background: var(--background-odd);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(even) {\n",
       "    background: var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:hover {\n",
       "    background: var(--background-hover);\n",
       "}\n",
       "\n",
       "table.dataframe a {\n",
       "    cursor: pointer;\n",
       "    color: var(--link-color);\n",
       "    text-decoration: none;\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover > td a {\n",
       "    color: var(--link-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe a:hover {\n",
       "    color: var(--link-hover);\n",
       "    text-decoration: underline;\n",
       "}\n",
       "\n",
       "table.dataframe img {\n",
       "    max-width: fit-content;\n",
       "}\n",
       "\n",
       "table.dataframe th.complex {\n",
       "    background-color: var(--background);\n",
       "    border: 1px solid var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe .leftBorder {\n",
       "    border-left-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightBorder {\n",
       "    border-right-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightAlign {\n",
       "    text-align: right;\n",
       "}\n",
       "\n",
       "table.dataframe .expanderSvg {\n",
       "    width: 8px;\n",
       "    height: 8px;\n",
       "    margin-right: 3px;\n",
       "}\n",
       "\n",
       "table.dataframe .expander {\n",
       "    display: flex;\n",
       "    align-items: center;\n",
       "}\n",
       "\n",
       "/* formatting */\n",
       "\n",
       "table.dataframe .null {\n",
       "    color: var(--text-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe .structural {\n",
       "    color: var(--text-color-medium);\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .dataFrameCaption {\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .numbers {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe td:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "            </style>\n",
       "        </head>\n",
       "        <body>\n",
       "            \n",
       "<table class=\"dataframe\" id=\"df_-1711276032\"></table>\n",
       "\n",
       "<p class=\"dataframe_description\">DataFrame: rowsCount = 5, columnsCount = 12</p>\n",
       "        </body>\n",
       "        <script>\n",
       "            \n",
       "/*<!--*/\n",
       "call_DataFrame(function() { DataFrame.addTable({ cols: [{ name: \"<span title=\\\"untitled: Int\\\">untitled</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">2</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">3</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">4</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">5</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"manufacturer: String\\\">manufacturer</span>\", children: [], rightAlign: false, values: [\"audi\",\"audi\",\"audi\",\"audi\",\"audi\"] }, \n",
       "{ name: \"<span title=\\\"model: String\\\">model</span>\", children: [], rightAlign: false, values: [\"a4\",\"a4\",\"a4\",\"a4\",\"a4\"] }, \n",
       "{ name: \"<span title=\\\"displ: Double\\\">displ</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1,8</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1,8</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">2,0</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">2,0</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">2,8</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"year: Int\\\">year</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1999</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1999</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">2008</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">2008</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1999</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"cyl: Int\\\">cyl</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">4</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">4</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">4</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">4</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">6</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"trans: String\\\">trans</span>\", children: [], rightAlign: false, values: [\"auto(l5)\",\"manual(m5)\",\"manual(m6)\",\"auto(av)\",\"auto(l5)\"] }, \n",
       "{ name: \"<span title=\\\"drv: String\\\">drv</span>\", children: [], rightAlign: false, values: [\"f\",\"f\",\"f\",\"f\",\"f\"] }, \n",
       "{ name: \"<span title=\\\"cty: Int\\\">cty</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">18</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">21</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">20</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">21</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">16</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"hwy: Int\\\">hwy</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">29</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">29</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">31</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">30</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">26</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"fl: String\\\">fl</span>\", children: [], rightAlign: false, values: [\"p\",\"p\",\"p\",\"p\",\"p\"] }, \n",
       "{ name: \"<span title=\\\"class: String\\\">class</span>\", children: [], rightAlign: false, values: [\"compact\",\"compact\",\"compact\",\"compact\",\"compact\"] }, \n",
       "], id: -1711276032, rootId: -1711276032, totalRows: 5 } ) });\n",
       "/*-->*/\n",
       "\n",
       "call_DataFrame(function() { DataFrame.renderTable(-1711276032) });\n",
       "\n",
       "\n",
       "        </script>\n",
       "        </html>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "var mpgDf = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv\")\n",
    "mpgDf.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "362a49d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "val mpgData = mpgDf.toMap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c0ca3a3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "val blankTheme = theme(line = elementBlank(), axis = elementBlank())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec0e815f",
   "metadata": {},
   "source": [
    "Prepare the tooltips content to show the variables calculated by the statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4769236b",
   "metadata": {},
   "outputs": [],
   "source": [
    "val tooltipContent = layerTooltips()\n",
    "                        .title(\"^fill\")\n",
    "                        .line(\"count of records in group|@{..count..} (@{..prop..})\")\n",
    "                        .line(\"total count of records|@{..sum..}\")\n",
    "                        .format(\"..prop..\", \".0%\")\n",
    "                        .format(\"..count..\", \".1f\")\n",
    "                        .format(\"..sum..\", \".1f\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b741fe7f",
   "metadata": {},
   "source": [
    "### 2. Default presentation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91e9ad66",
   "metadata": {},
   "source": [
    "Let's show the variables computed by the in the tooltips."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c707c39a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"zsFTfb\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"manufacturer\",\n",
       "\"fill\":\"class\"\n",
       "},\n",
       "\"stat\":\"count\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"tooltips\":{\n",
       "\"formats\":[{\n",
       "\"field\":\"..prop..\",\n",
       "\"format\":\".0%\"\n",
       "},{\n",
       "\"field\":\"..count..\",\n",
       "\"format\":\".1f\"\n",
       "},{\n",
       "\"field\":\"..sum..\",\n",
       "\"format\":\".1f\"\n",
       "}],\n",
       "\"title\":\"^fill\",\n",
       "\"lines\":[\"count of records in group|@{..count..} (@{..prop..})\",\"total count of records|@{..sum..}\"]\n",
       "},\n",
       "\"data\":{\n",
       "\"..sum..\":[18.0,13.0,14.0,34.0,27.0,18.0,19.0,14.0,13.0,5.0,34.0,27.0,19.0,37.0,25.0,8.0,4.0,3.0,4.0,13.0,14.0,34.0,19.0,37.0,37.0,25.0,34.0,25.0,9.0,14.0,14.0,27.0],\n",
       "\"..count..\":[15.0,2.0,4.0,12.0,14.0,3.0,5.0,7.0,7.0,5.0,7.0,7.0,9.0,7.0,9.0,8.0,4.0,3.0,4.0,4.0,6.0,8.0,5.0,11.0,19.0,7.0,7.0,9.0,9.0,7.0,4.0,6.0],\n",
       "\"class\":[\"compact\",\"compact\",\"compact\",\"compact\",\"compact\",\"midsize\",\"midsize\",\"midsize\",\"midsize\",\"midsize\",\"midsize\",\"midsize\",\"suv\",\"suv\",\"suv\",\"suv\",\"suv\",\"suv\",\"suv\",\"suv\",\"suv\",\"suv\",\"2seater\",\"minivan\",\"pickup\",\"pickup\",\"pickup\",\"subcompact\",\"subcompact\",\"subcompact\",\"subcompact\",\"subcompact\"],\n",
       "\"..prop..\":[0.8333333333333334,0.15384615384615385,0.2857142857142857,0.35294117647058826,0.5185185185185185,0.16666666666666666,0.2631578947368421,0.5,0.5384615384615384,1.0,0.20588235294117646,0.25925925925925924,0.47368421052631576,0.1891891891891892,0.36,1.0,1.0,1.0,1.0,0.3076923076923077,0.42857142857142855,0.23529411764705882,0.2631578947368421,0.2972972972972973,0.5135135135135135,0.28,0.20588235294117646,0.36,1.0,0.5,0.2857142857142857,0.2222222222222222],\n",
       "\"manufacturer\":[\"audi\",\"nissan\",\"subaru\",\"toyota\",\"volkswagen\",\"audi\",\"chevrolet\",\"hyundai\",\"nissan\",\"pontiac\",\"toyota\",\"volkswagen\",\"chevrolet\",\"dodge\",\"ford\",\"jeep\",\"land rover\",\"lincoln\",\"mercury\",\"nissan\",\"subaru\",\"toyota\",\"chevrolet\",\"dodge\",\"dodge\",\"ford\",\"toyota\",\"ford\",\"honda\",\"hyundai\",\"subaru\",\"volkswagen\"]\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"zsFTfb\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ggplot(mpgData) +\n",
    "    geomBar(tooltips = tooltipContent) { x = \"manufacturer\"; fill = \"class\" }"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c525cb5b",
   "metadata": {},
   "source": [
    "Let's build a pie chart where the sector will correspond to the car class.\n",
    "All statistical information is shown in tooltips."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4a2856c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"ppr5qJ\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"hole\":0.2,\n",
       "\"mapping\":{\n",
       "\"fill\":\"class\"\n",
       "},\n",
       "\"stat\":\"count2d\",\n",
       "\"size\":24.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"pie\",\n",
       "\"stroke\":1.0,\n",
       "\"tooltips\":{\n",
       "\"formats\":[{\n",
       "\"field\":\"..prop..\",\n",
       "\"format\":\".0%\"\n",
       "},{\n",
       "\"field\":\"..count..\",\n",
       "\"format\":\".1f\"\n",
       "},{\n",
       "\"field\":\"..sum..\",\n",
       "\"format\":\".1f\"\n",
       "}],\n",
       "\"title\":\"^fill\",\n",
       "\"lines\":[\"count of records in group|@{..count..} (@{..prop..})\",\"total count of records|@{..sum..}\"]\n",
       "},\n",
       "\"data\":{\n",
       "\"..sum..\":[234.0,234.0,234.0,234.0,234.0,234.0,234.0],\n",
       "\"..x..\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0],\n",
       "\"..count..\":[47.0,41.0,62.0,5.0,11.0,33.0,35.0],\n",
       "\"..y..\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0],\n",
       "\"..prop..\":[0.20085470085470086,0.1752136752136752,0.26495726495726496,0.021367521367521368,0.04700854700854701,0.14102564102564102,0.14957264957264957],\n",
       "\"class\":[\"compact\",\"midsize\",\"suv\",\"2seater\",\"minivan\",\"pickup\",\"subcompact\"]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"axis\":{\n",
       "\"blank\":true\n",
       "},\n",
       "\"line\":{\n",
       "\"blank\":true\n",
       "}\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"ppr5qJ\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ggplot(mpgData) +\n",
    "    geomPie(size=24, hole=0.2, stroke=1.0, tooltips = tooltipContent) { fill = \"class\" } +\n",
    "    blankTheme"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "664e6b9f",
   "metadata": {},
   "source": [
    "### 3. Compute weighted sum"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b10fc659",
   "metadata": {},
   "source": [
    "Compute weighted sum instead of simple count: let's use total engine displacement of each class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "21c837af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"v6uC6E\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"hole\":0.2,\n",
       "\"mapping\":{\n",
       "\"fill\":\"class\",\n",
       "\"weight\":\"displ\"\n",
       "},\n",
       "\"stat\":\"count2d\",\n",
       "\"size\":24.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"pie\",\n",
       "\"stroke\":1.0,\n",
       "\"tooltips\":{\n",
       "\"formats\":[{\n",
       "\"field\":\"..prop..\",\n",
       "\"format\":\".0%\"\n",
       "},{\n",
       "\"field\":\"..count..\",\n",
       "\"format\":\".1f\"\n",
       "},{\n",
       "\"field\":\"..sum..\",\n",
       "\"format\":\".1f\"\n",
       "}],\n",
       "\"title\":\"^fill\",\n",
       "\"lines\":[\"count of records in group|@{..count..} (@{..prop..})\",\"total count of records|@{..sum..}\"]\n",
       "},\n",
       "\"data\":{\n",
       "\"..sum..\":[812.3999999999999,812.3999999999999,812.3999999999999,812.3999999999999,812.3999999999999,812.3999999999999,812.3999999999999],\n",
       "\"..x..\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0],\n",
       "\"..count..\":[109.29999999999998,119.79999999999998,276.2999999999999,30.8,37.300000000000004,145.80000000000004,93.10000000000002],\n",
       "\"..y..\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0],\n",
       "\"..prop..\":[0.1345396356474643,0.14746430329886756,0.34010339734121114,0.03791235844411621,0.04591334318069917,0.1794682422451995,0.11459871984244219],\n",
       "\"class\":[\"compact\",\"midsize\",\"suv\",\"2seater\",\"minivan\",\"pickup\",\"subcompact\"]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"axis\":{\n",
       "\"blank\":true\n",
       "},\n",
       "\"line\":{\n",
       "\"blank\":true\n",
       "}\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"v6uC6E\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ggplot(mpgData) +\n",
    "    geomPie(size=24, hole=0.2, stroke=1.0, tooltips = tooltipContent) { \n",
    "        fill = \"class\"\n",
    "        weight = \"displ\"\n",
    "    } +\n",
    "    blankTheme"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce251ba0",
   "metadata": {},
   "source": [
    "Then let's order sectors by `'..count..'`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "17be9143",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"NdPgml\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"hole\":0.2,\n",
       "\"mapping\":{\n",
       "\"fill\":\"@as_discrete@class\",\n",
       "\"weight\":\"displ\"\n",
       "},\n",
       "\"stat\":\"count2d\",\n",
       "\"size\":24.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"pie\",\n",
       "\"stroke\":1.0,\n",
       "\"data_meta\":{\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"fill\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"class\",\n",
       "\"order_by\":\"..count..\"\n",
       "}\n",
       "}]\n",
       "},\n",
       "\"tooltips\":{\n",
       "\"formats\":[{\n",
       "\"field\":\"..prop..\",\n",
       "\"format\":\".0%\"\n",
       "},{\n",
       "\"field\":\"..count..\",\n",
       "\"format\":\".1f\"\n",
       "},{\n",
       "\"field\":\"..sum..\",\n",
       "\"format\":\".1f\"\n",
       "}],\n",
       "\"title\":\"^fill\",\n",
       "\"lines\":[\"count of records in group|@{..count..} (@{..prop..})\",\"total count of records|@{..sum..}\"]\n",
       "},\n",
       "\"data\":{\n",
       "\"..sum..\":[812.3999999999997,812.3999999999997,812.3999999999997,812.3999999999997,812.3999999999997,812.3999999999997,812.3999999999997],\n",
       "\"..x..\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0],\n",
       "\"..count..\":[276.2999999999999,145.80000000000004,119.79999999999998,109.29999999999998,93.10000000000002,37.300000000000004,30.8],\n",
       "\"..y..\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0],\n",
       "\"@as_discrete@class\":[\"suv\",\"pickup\",\"midsize\",\"compact\",\"subcompact\",\"minivan\",\"2seater\"],\n",
       "\"..prop..\":[0.3401033973412112,0.1794682422451995,0.1474643032988676,0.13453963564746432,0.11459871984244221,0.04591334318069918,0.03791235844411621]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"axis\":{\n",
       "\"blank\":true\n",
       "},\n",
       "\"line\":{\n",
       "\"blank\":true\n",
       "}\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"NdPgml\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ggplot(mpgData) +\n",
    "    geomPie(size=24, hole=0.2, stroke=1.0, tooltips = tooltipContent) { \n",
    "        fill = asDiscrete(\"class\", orderBy = \"..count..\")\n",
    "        weight = \"displ\"\n",
    "    } +\n",
    "    blankTheme"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "224e352f",
   "metadata": {},
   "source": [
    "Map `size` to computed variable `'..sum..'`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b384e38d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"Kz4QFH\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"guides\":{\n",
       "\"size\":\"none\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"size\",\n",
       "\"range\":[4.0,10.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"size\":\"..sum..\",\n",
       "\"fill\":\"class\"\n",
       "},\n",
       "\"stat\":\"count2d\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"pie\",\n",
       "\"tooltips\":{\n",
       "\"formats\":[{\n",
       "\"field\":\"..prop..\",\n",
       "\"format\":\".0%\"\n",
       "},{\n",
       "\"field\":\"..count..\",\n",
       "\"format\":\".1f\"\n",
       "},{\n",
       "\"field\":\"..sum..\",\n",
       "\"format\":\".1f\"\n",
       "}],\n",
       "\"title\":\"^fill\",\n",
       "\"lines\":[\"count of records in group|@{..count..} (@{..prop..})\",\"total count of records|@{..sum..}\"]\n",
       "},\n",
       "\"data\":{\n",
       "\"..sum..\":[18.0,18.0,19.0,19.0,19.0,37.0,37.0,37.0,25.0,25.0,25.0,9.0,14.0,14.0,8.0,4.0,3.0,4.0,13.0,13.0,13.0,5.0,14.0,14.0,14.0,34.0,34.0,34.0,34.0,27.0,27.0,27.0],\n",
       "\"..x..\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],\n",
       "\"..count..\":[15.0,3.0,5.0,9.0,5.0,7.0,11.0,19.0,9.0,7.0,9.0,9.0,7.0,7.0,8.0,4.0,3.0,4.0,2.0,7.0,4.0,5.0,4.0,6.0,4.0,12.0,7.0,8.0,7.0,14.0,7.0,6.0],\n",
       "\"..y..\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],\n",
       "\"..prop..\":[0.8333333333333334,0.16666666666666666,0.2631578947368421,0.47368421052631576,0.2631578947368421,0.1891891891891892,0.2972972972972973,0.5135135135135135,0.36,0.28,0.36,1.0,0.5,0.5,1.0,1.0,1.0,1.0,0.15384615384615385,0.5384615384615384,0.3076923076923077,1.0,0.2857142857142857,0.42857142857142855,0.2857142857142857,0.35294117647058826,0.20588235294117646,0.23529411764705882,0.20588235294117646,0.5185185185185185,0.25925925925925924,0.2222222222222222],\n",
       "\"class\":[\"compact\",\"midsize\",\"midsize\",\"suv\",\"2seater\",\"suv\",\"minivan\",\"pickup\",\"suv\",\"pickup\",\"subcompact\",\"subcompact\",\"midsize\",\"subcompact\",\"suv\",\"suv\",\"suv\",\"suv\",\"compact\",\"midsize\",\"suv\",\"midsize\",\"compact\",\"suv\",\"subcompact\",\"compact\",\"midsize\",\"suv\",\"pickup\",\"compact\",\"midsize\",\"subcompact\"],\n",
       "\"manufacturer\":[\"audi\",\"audi\",\"chevrolet\",\"chevrolet\",\"chevrolet\",\"dodge\",\"dodge\",\"dodge\",\"ford\",\"ford\",\"ford\",\"honda\",\"hyundai\",\"hyundai\",\"jeep\",\"land rover\",\"lincoln\",\"mercury\",\"nissan\",\"nissan\",\"nissan\",\"pontiac\",\"subaru\",\"subaru\",\"subaru\",\"toyota\",\"toyota\",\"toyota\",\"toyota\",\"volkswagen\",\"volkswagen\",\"volkswagen\"]\n",
       "},\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"column\":\"class\",\n",
       "\"factor_levels\":[\"compact\",\"midsize\",\"suv\",\"2seater\",\"minivan\",\"pickup\",\"subcompact\"]\n",
       "}]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"axis\":{\n",
       "\"blank\":true\n",
       "},\n",
       "\"line\":{\n",
       "\"blank\":true\n",
       "}\n",
       "},\n",
       "\"facet\":{\n",
       "\"name\":\"wrap\",\n",
       "\"ncol\":5.0,\n",
       "\"dir\":\"h\",\n",
       "\"facets\":\"manufacturer\",\n",
       "\"order\":1.0\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"Kz4QFH\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ggplot(mpgData) +\n",
    "    geomPie(tooltips = tooltipContent) { fill = \"class\"; size = \"..sum..\" } +\n",
    "    facetWrap(facets = \"manufacturer\", ncol = 5, order = 1) +\n",
    "    scaleSize(range = 4 to 10) +\n",
    "    guides(size = \"none\") +\n",
    "    blankTheme"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
